<template>
  <div class="header">
    <div class="system-name">
        >> {{ title }}
    </div>
    <el-dropdown>
        <span class="el-dropdown-link">
            <el-avatar>{{ role === 'admin' ? 'admin' : 'agency' }}</el-avatar>
            <el-icon><arrow-down /></el-icon>
        </span>
        <template #dropdown>
            <el-dropdown-menu>
                <el-dropdown-item @click="toLeave">退出登录</el-dropdown-item>
            </el-dropdown-menu>
        </template>
    </el-dropdown>
  </div>
</template>

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from 'element-plus';
import { onMounted, ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
import { ArrowDown } from '@element-plus/icons-vue'

// 获取当前机构名
const { role } = JSON.parse(localStorage.getItem('user') || "");
const title = ref(''); // 当前页标题
const route = useRoute();
onMounted(() => {
    // 初始化设置
    title.value = route.meta.title as string;
    // 监听路由变化 更新标题
    watchEffect(() => {
        title.value = route.meta.title as string;
    })
})

const router = useRouter();
/** 退出登录 */
const toLeave = () => {
    ElMessageBox.confirm(
        '确认退出登录吗？',
        '危险操作',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }
    ).then(() => {
        localStorage.removeItem('user'); // 清空保存的信息
        // 跳转到登陆界面
        router.push('/login');
        ElMessage({
            type: 'success',
            message: '退出登录成功！'
        })
    });
}
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}
.system-name {
  font-size: 20px;
  font-weight: bold;
  color: #3c89d4;
}
.el-dropdown-link {
  cursor: pointer;
  display: inline-block;
  padding: 0 20px;
  height: 100%;
  line-height: 60px;
  color: #000000;
  align-items: center;
  border: none !important;
  outline: none;
  box-shadow: none;
}
</style>